home *** CD-ROM | disk | FTP | other *** search
- #define ERROR -1
- /*-------------------------------------------------------------
- RESORT is a utility which maintains a user dictionary SPELL.DIC
- in alpha order. This works in conjunction with the utility
- SPELL.COM, by Michael C. Adler, as modified for the Osborne
- User Groups.
-
- RESORT is Copyright (C) 1983 by Donald G. Krantz. No commercial
- use may be made of this program without permission of Author.
-
- Limited license is granted for non-commercial distribution of
- this program in either source or object form.
- ---------------------------------------------------------------
- -------------------------------------------------------------*/
-
- #define VERSION "RESORT Version 1.1 (C) 1984 Donald G. Krantz"
- #include "stdio.h"
-
- /*------------------------------------------------------------
- stucture node is the basic data type used to describe the
- sorted array of words.
- ------------------------------------------------------------*/
-
- struct node {
- struct node *lower;
- struct node *higher;
- char *word;
- };
-
- struct node *root; /* root is the root of the binary tree */
-
- FILE *inptr; /* file descriptor to input files */
- FILE *outptr; /* file descriptor to output file */
-
- main( argc, argv )
- int argc;
- char *argv[];
- {
- char tempfile[ 20 ]; /* temporary text filespec */
- char bakfile[ 20 ]; /* .BAK filespec */
-
- printf("\n\s\n", VERSION );
- /* check command line argument */
- if( argc != 2 ) /* one argument only... */
- {
- printf("\nERROR - Use:\nRESORT d:filename.typ\n\n");
- exit( 1 );
- }
- /* open input file */
- if( (inptr = fopen( argv[1], "r", 512 )) == NULL )
- {
- printf("\nERROR - Can't open file \s\n",argv[1]);
- exit( 1 );
- }
- /* make .BAK and .$$$ file spec and open temps */
- makebak( argv[ 1 ], bakfile, tempfile );
- unlink( tempfile ); /* dump old temps */
- if( (outptr = fopen( tempfile, "w" ) ) == NULL )
- {
- printf( "\nERROR - Can't open temporary file\n" );
- exit( 1 );
- }
- /* sort input file and strip tildes, transfer to temp file */
- root = sort( NULL );
- /* change temp filename to original, original to .BAK */
- fclose( inptr ); /* close input */
- if( fclose( outptr ) == ERROR) /* close temp */
- {
- printf( "\nERROR - Disk full - aborting\n" );
- unlink( tempfile );
- exit( 1 );
- }
- unlink( bakfile ); /* dump old .BAK */
- /* rename: rename( new_file_spec, old_file_spec ) */
- rename( bakfile, argv[1] ); /* make new .BAK */
- rename( argv[1], tempfile ); /* make new text */
- if( root == NULL ) /* no new words */
- exit( 0 ); /* so quit */
- /* sort .DIC file into binary tree */
- if( (inptr = fopen( "SPELL.DIC", "r" )) == NULL )
- {
- printf( "\nERROR - Can't open dictionary file\n" );
- exit( 1 );
- }
- root = sort2( root ); /* sort .DIC */
- fclose( inptr ); /* dump .DIC */
- /* re-write .DIC file */
- if( (inptr = fopen( "SPELL.DIC", "w", 512 )) == NULL )
- {
- printf( "\nERROR - Can't write to dictionary file");
- exit( 1 );
- }
- /* write sorted tree to .DIC file */
- print( root );
- if( fclose( inptr ) == ERROR )
- {
- printf("\nERROR - Disk full - Recheck SPELL.DIC\n");
- exit( 1 );
- }
- }
-
-
- /*---------------------------------------------------------------
- makebak() creates filespecs for a temporary file and a backup file
- to be used in filtering applications like RESORT. It's kind of
- a generic function.
- ---------------------------------------------------------------*/
-
- makebak( orgfile, bakfile, tempfile )
- char *orgfile, *bakfile, *tempfile;
- {
- strcpy( tempfile, orgfile );
- strcpy( bakfile, orgfile );
- if( index( tempfile, '.' ) == NULL ) /*.TYP spec'ed? */
- { /* no */
- strcat( tempfile, ".$$$" );
- strcat( bakfile, ".BAK" );
- }
- else /* yes */
- {
- strcpy( index( tempfile, '.'), ".$$$");
- strcpy( index( bakfile, '.'), ".BAK");
- }
- }
-
-
- /*---------------------------------------------------------------
- sort() reads the input file and puts the words with leading
- tildes into the binary tree.
- ---------------------------------------------------------------*/
-
- struct node *
- sort( p )
- struct node *p;
- {
- static char w[100];
- register int i;
- static int c;
-
- while( TRUE )
- {
- /* find tilde */
- while((((c = getcc( inptr )) & 0x7F) != '~') &&
- (c != ERROR))
- ;
- /* accumulate subsequent word into 'w' */
- i = 0; /* i = letter count in word */
- while( (i == 0) || (isalpha( c ) ) )
- {
- c = getcc( inptr );
- if( c == ERROR )
- return( p );
- c = toupper( 0x7F & c );
- if( isalpha( c ) == FALSE )
- break;
- w[ i++ ] = c;
- }
- if( i == 0 )
- continue;
- w[ i ] = '\0';
- /* put word into binary tree */
- p = treeload( w, p );
- }
- }
-
-
- /*---------------------------------------------------------------
- sort2() reads the .DIC file and puts the words into the binary
- tree.
- ---------------------------------------------------------------*/
-
- struct node *
- sort2( p )
- struct node *p;
- {
- static char w[100];
- register int i;
- static int c;
-
- while( TRUE )
- {
- i = 0; /* i=letter count in word */
- /* loop for alphabetic data, skip non-alpha */
- while((i == 0) || (isalpha( c )))
- {
- c = getc( inptr );
- if( c == ERROR )
- return( p );
- c = toupper( 0x7F & c );
- if( isalpha( c ) == FALSE )
- break;
- w[ i++ ] = c;
- }
- if( i == 0 )
- continue;
- w[ i ] = '\0';
- /* put word into binary tree */
- p = treeload( w, p );
- }
- }
-
-
- /*-------------------------------------------------------------
- treeload( w, p ) loads the tree with words, placing them in
- alphabetical order.
- -------------------------------------------------------------*/
-
- struct node *
- treeload( w, p )
- char *w;
- struct node *p;
- {
-
- if( p == NULL )
- /* we are at end of tree, need to add a node for this word */
- {
- if((p = alloc( sizeof( struct node ) )) == NULL)
- {
- printf("\nERROR - Out of memory");
- exit( 1 );
- }
- if((p->word = alloc( strlen( w ) + 1 )) == NULL)
- {
- printf("\nERROR - Out of memory");
- exit( 1 );
- }
- p->lower = NULL;
- p->higher = NULL;
- strcpy( p->word, w );
- return( p );
- }
- else if( strcmp( p->word, w ) > 0 )
- /* word 'w' is lower than the word at this node, look lower */
- p->lower = treeload( w, p->lower );
- else if( strcmp( p->word, w ) < 0 )
- /* word 'w' is higher than the word at this node, look higher */
- p->higher = treeload( w, p->higher );
- /* if word 'w' is identical to the word at this node, we do */
- /* nothing. In all cases, we return the current node pointer. */
- return( p );
- }
-
-
- /*--------------------------------------------------------------
- print() writes the words in alpha order back into the .DIC
- file.
- --------------------------------------------------------------*/
-
- print( p )
- struct node *p;
- {
- /* print all words lower than current node first */
- if( p->lower != NULL )
- print( p->lower );
-
- /* print the word at the current node */
- fprintf( inptr, "\s\n", p->word );
-
- /* print all words higher than the current node */
- if( p->higher != NULL )
- print( p->higher );
- }
-
-
- /*-------------------------------------------------------------
- getcc() gets chars from the infile, and writes all but tildes
- to the outfile. This function does not disturb any high bits
- set for parity or formatting.
- -------------------------------------------------------------*/
-
- getcc( fd )
- int *fd;
- {
- int c;
- c = getc( fd );
- if(((c & 0x7F) != '~') && (c != ERROR))
- if( fputc( c, outptr ) == ERROR)
- {
- printf("\nERROR - Disk full - Pass 1");
- exit( 1 );
- }
- return( c );
- }